package com.own.core.html.panel;

import org.apache.wicket.Component;
import org.apache.wicket.extensions.breadcrumb.BreadCrumbLink;
import org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel;
import org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant;
import org.apache.wicket.extensions.breadcrumb.panel.BreadCrumbParticipantDelegate;
import org.apache.wicket.extensions.breadcrumb.panel.IBreadCrumbPanelFactory;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel; 

/**
 * A panel that participates with a {@link IBreadCrumbModel bread crumb model}.
 * The idea is that you would have a dialog-like component that is much like a
 * wizard, but more decoupled. A typical setup is that you have a panel, where
 * the content is dynamic but hierarchical in nature, and that there are links
 * on the panel that <i>take you deeper into the hierarchy<i>
 * 
 * <p>
 * An example of using {@link OwnBreadCrumbPanel bread crumb panels} and
 * {@link BreadCrumbLink bread crumb links}:
 * 
 * <pre>
 * add(new BreadCrumbLink(&quot;myLink&quot;, breadCrumbModel) {
 * 	protected IBreadCrumbParticipant getParticipant(String componentId) {
 * 		return new MyPanel(componentId, breadCrumbModel);
 * 	}
 * });
 * </pre>
 * 
 * where <tt>MyPanel</tt> is a {@link OwnBreadCrumbPanel bread crumb panel} and
 * the link is added to another {@link OwnBreadCrumbPanel bread crumb panel}
 * instance (this). When clicked, MyPanel will replace the panel that the link
 * is placed on, and it will set (and add) <tt>MyPanel</tt> as the active bread
 * crumb in the {@link IBreadCrumbModel bread crumb component model}.
 * </p>
 * 
 * @author Eelco Hillenius
 */
public  class OwnBreadCrumbPanel extends Panel implements
		IBreadCrumbParticipant {
	private static final long serialVersionUID = 1L;

	/** The bread crumb model. */
	private IBreadCrumbModel breadCrumbModel;

	private OwnPanel parentPanel;

	
	private String title="";
	/**
	 * Implementation of the participant.
	 */
	private final BreadCrumbParticipantDelegate decorated = new BreadCrumbParticipantDelegate(
			this) {
		private static final long serialVersionUID = 1L;

		public String getTitle() {
			return OwnBreadCrumbPanel.this.getTitle();
		}
	};

	/**
	 * Construct.
	 * 
	 * @param id
	 *            Component id
	 * @param breadCrumbModel
	 *            The bread crumb model
	 */
	public OwnBreadCrumbPanel(String id) {
		super(id);
		 
	}

	/**
	 * Construct.
	 * 
	 * @param id
	 *            Component id
	 * @param breadCrumbModel
	 *            The bread crumb model
	 * @param model
	 *            The model
	 */
	public OwnBreadCrumbPanel(String id, IModel<?> model) {
		super(id, model);
	 
	}

	/**
	 * Activates the {@link OwnBreadCrumbPanel bread crumb panel} that is the
	 * result of calling
	 * {@link IBreadCrumbPanelFactory#create(String, IBreadCrumbModel) the
	 * create method} of the bread crumb panel factory.
	 * 
	 * @param breadCrumbPanelFactory
	 */
	public void activate(IBreadCrumbPanelFactory breadCrumbPanelFactory) {
		activate(breadCrumbPanelFactory.create(getId(), getBreadCrumbModel()));
	}

	/**
	 * Activates the provided participant, which typically has the effect of
	 * replacing this current panel with the one provided - as the participant
	 * typically would be a {@link OwnBreadCrumbPanel bread crumb panel} - and
	 * updating the bread crumb model of this panel, pushing the bread crumb for
	 * the given participant on top.
	 * 
	 * @param participant
	 *            The participant to set as the active oneOwnBreadCrumbPanel
	 */
	public void activate(IBreadCrumbParticipant participant) {
		// get the currently active participant
				final IBreadCrumbParticipant active = breadCrumbModel.getActive();
				if (active == null)
				{
					throw new IllegalStateException("The model has no active bread crumb. Before using " +
						this + ", you have to have at least one bread crumb in the model");
				}

				// add back button support
				addStateChange();

				// set the bread crumb panel as the active one
				breadCrumbModel.setActive(participant);
	}

	/**
	 * Gets the bread crumb panel.
	 * 
	 * @return The bread crumb panel
	 */
	public final IBreadCrumbModel getBreadCrumbModel() {
		if(breadCrumbModel==null){
			return parentPanel.getBreadCrumbModel();
			
		}
		return breadCrumbModel;
	}

	/**
	 * The participating component == this.
	 * 
	 * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant#getComponent()
	 */
	public Component getComponent() {
		return decorated.getComponent();
	}

	/**
	 * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant#onActivate(org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant)
	 */
	public void onActivate(IBreadCrumbParticipant previous) {
		decorated.onActivate(previous);
	}

	/**
	 * Sets the bread crumb panel.
	 * 
	 * @param breadCrumbModel
	 *            The bread crumb panel
	 */
	public final void setBreadCrumbModel(IBreadCrumbModel breadCrumbModel) {
		this.breadCrumbModel = breadCrumbModel;
	}

	public OwnPanel getParentPanel() {
		return parentPanel;
	}

	public void setParentPanel(OwnPanel parentPanel) {
		this.parentPanel = parentPanel;
	}

	public String getTitle() {
		if(title==null&&!title.equals("")){
			return parentPanel.getTitle();
			
		}
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

 
}